package com.base.power.demo.sys.service;

import com.base.power.demo.common.base.constant.SystemStaticConst;
import com.base.power.demo.common.base.dao.GenericDao;
import com.base.power.demo.common.base.service.GenericService;
import com.base.power.demo.common.util.node.NodeUtil;
import com.base.power.demo.common.util.user.UserInfo;
import com.base.power.demo.sys.dao.TreeDao;
import com.base.power.demo.sys.dao.UserDao;
import com.base.power.demo.sys.dao.UserRelationRoleDao;
import com.base.power.demo.sys.entity.*;
import com.base.power.demo.sys.vo.RegPassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 类描述：用户业务逻辑实现类
 */
@Service
@Transactional(rollbackFor={IllegalArgumentException.class})
public class UserService extends GenericService<User,QueryUser> {

    @Autowired
    private UserDao userDao;
    @Autowired
    private TreeDao treeDao;
    @Autowired
    private UserRelationRoleDao userRelationRoleDao;

    @Override
    protected GenericDao<User, QueryUser> getDao() {
        return this.userDao;
    }

    /**
     * 功能描述：更新用户状态为可用或者不可用
     * @param user
     * @return
     */
    public boolean userControl(User user){
        return userDao.userControl(user)>0;
    }

    /**
     * 功能描述：修改用户信息
     * @param entity
     * @return
     */
    public Map<String,Object> regUserInfo(User entity){
        Map<String,Object> result = new HashMap<String,Object>();
        User login = UserInfo.getUser();
        if(login==null){
            result.put(SystemStaticConst.MSG, "用户未登陆");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            return result;
        }
        entity.setId(login.getId());
        if(userDao.regUserInfo(entity)==0){
            result.put(SystemStaticConst.MSG, "修改个人信息失败");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
        }else{
            result.put(SystemStaticConst.MSG, "修改个人信息成功");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
        }
        return result;
    }

    /**
     * 功能描述：修改当前登陆用户的密码
     * @param entity
     * @return
     */
    public Map<String,Object> regPass(RegPassVo entity){
        Map<String,Object> result = new HashMap<String,Object>();
        User user = UserInfo.getUser();
        if(user==null){
            result.put(SystemStaticConst.MSG, "用户未登陆");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            return result;
        }
        if(!UserInfo.maths(entity.getOldPass(),user.getPassword())){
            result.put(SystemStaticConst.MSG, "用户密码错误");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            return result;
        }
        user.setPass(UserInfo.encode(entity.getNewPass()));
        if(userDao.regPass(user)==0){
            result.put(SystemStaticConst.MSG, "修改密码失败");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
        }else{
            result.put(SystemStaticConst.MSG, "修改密码成功");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
        }
        return result;
    }

    /**
     * 功能描述：根据账号来获取用户信息
     * @param login
     * @return
     */
    public User findByLogin(String login){
        return userDao.findByLogin(login);
    }

    /**
     * 功能描述：实现更新用户
     * @param entity 修改对象
     * @return
     * @throws Exception
     */
    @Override
    public boolean update(User entity) throws Exception {
        entity.packagingRoles(entity.getRoleArray());
        userRelationRoleDao.removeUserRole(entity);
        if(entity.getRoles().size()>0){
            for(UserRole userRole:entity.getRoles()){
                userRelationRoleDao.save(new UserRelationRole(entity.getId(),userRole.getId()));
            }
        }
        return super.update(entity);
    }

    /**
     * 功能描述：批量删除用户
     * @param entityList
     * @return
     * @throws Exception
     */
    @Override
    public boolean removeBath(List<User> entityList) throws Exception {
        for(User user:entityList){
            userRelationRoleDao.removeUserRole(user);
        }
        return super.removeBath(entityList);
    }

    /**
     * 功能描述：重写保存用户的方法
     * @param entity 保存对象
     * @return
     * @throws Exception
     */
    @Override
    public boolean save(User entity) throws Exception {
        entity.packagingRoles(entity.getRoleArray());
        entity.setCrtDate(new Date());
        entity.setPass(UserInfo.encode(entity.getPass()));
        boolean success = super.save(entity);
        // 保存用户与角色关联的数据
        List<UserRole> userRoleList = entity.getRoles();
        if(success){
            if(userRoleList.size()>0){
                for(UserRole userRole:userRoleList){
                    userRelationRoleDao.save(new UserRelationRole(entity.getId(),userRole.getId()));
                }
            }
        }
        return success;
    }

    /**
     * 功能描述：根据当前登陆的用户来获取当前用户的菜单节点的数据
     * @return
     */
    public List<Tree> mainTree(){
        Map<String, Object> result = new HashMap<String, Object>();
        // 对菜单进行排重
        Map<Long,Tree> treeMap = new HashMap<Long,Tree>();
        // 获取当前登陆的用户的数据
        User user = UserInfo.getUser();
        if(user==null){
            result.put(SystemStaticConst.MSG, "用户未登陆");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            return null;
        }
        return  NodeUtil.getChildNodes(treeDao.loadUserTree(user),0l);
    }
}
